using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Mobileterminal.Common;
using Mobileterminal.Dtos;
using Newtonsoft.Json;
using Sunny.UI;

namespace Mobileterminal.Forms.Yjb
{
    public partial class MedicineInventoryForm : UIPage
    {
        public MedicineInventoryForm()
        {
            InitializeComponent();
            LoadMedicineInventoryDataAsync();
        }

        private async Task LoadMedicineInventoryDataAsync(string cardNo = null, string name = null, string dateRange = "全部")
        {
            try
            {
                // 构建动态查询参数
                var queryParams = new List<string>();
                if (!string.IsNullOrEmpty(cardNo))
                    queryParams.Add($"cardNo={Uri.EscapeDataString(cardNo)}");
                if (!string.IsNullOrEmpty(name))
                    queryParams.Add($"name={Uri.EscapeDataString(name)}");
                if (!string.IsNullOrEmpty(dateRange))
                    queryParams.Add($"dateRange={Uri.EscapeDataString(dateRange)}");

                var url = "https://localhost:7224/api/Show/ShowMedicineInventory";
                if (queryParams.Count > 0)
                    url += "?" + string.Join("&", queryParams);

                var json = await HttpClientHelper.ClientAsync("GET", url);
                var result = JsonConvert.DeserializeObject<Result<List<MedicineInventoryDTO>>>(json);

                if (result.Code == 200)
                {
                    // 设置数据源
                    uiDataGridView1.DataSource = result.Data;
                }
                else
                {
                    UIMessageTip.ShowError($"加载数据失败: {result.Msg}");
                }
            }
            catch (Exception ex)
            {
                UIMessageTip.ShowError($"发生错误: {ex.Message}");
            }
        }

        private async void btnSearch_Click(object sender, EventArgs e)
        {
            string cardNo = txtCardNo.Text.Trim();
            string name = txtName.Text.Trim();
            string dateRange = GetSelectedDateRange();
            await LoadMedicineInventoryDataAsync(cardNo, name, dateRange);
        }

        private string GetSelectedDateRange()
        {
            if (radioToday.Checked) return "今天";
            if (radioWeek.Checked) return "本周";
            if (radioMonth.Checked) return "本月";
            if (radioYear.Checked) return "本年";
            if (radioAll.Checked) return "全部";
            return "全部"; // 默认为全部
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            txtCardNo.Text = string.Empty;
            txtName.Text = string.Empty;
            radioAll.Checked = true;
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            txtCardNo.Text = string.Empty;
            txtName.Text = string.Empty;
            radioAll.Checked = true;
            LoadMedicineInventoryDataAsync();
        }

        private void radioToday_CheckedChanged(object sender, EventArgs e)
        {
            if (radioToday.Checked)
            {
                LoadMedicineInventoryDataAsync(txtCardNo.Text.Trim(), txtName.Text.Trim(), "今天");
            }
        }

        private void radioWeek_CheckedChanged(object sender, EventArgs e)
        {
            if (radioWeek.Checked)
            {
                LoadMedicineInventoryDataAsync(txtCardNo.Text.Trim(), txtName.Text.Trim(), "本周");
            }
        }

        private void radioMonth_CheckedChanged(object sender, EventArgs e)
        {
            if (radioMonth.Checked)
            {
                LoadMedicineInventoryDataAsync(txtCardNo.Text.Trim(), txtName.Text.Trim(), "本月");
            }
        }

        private void radioYear_CheckedChanged(object sender, EventArgs e)
        {
            if (radioYear.Checked)
            {
                LoadMedicineInventoryDataAsync(txtCardNo.Text.Trim(), txtName.Text.Trim(), "本年");
            }
        }

        private void radioAll_CheckedChanged(object sender, EventArgs e)
        {
            if (radioAll.Checked)
            {
                LoadMedicineInventoryDataAsync(txtCardNo.Text.Trim(), txtName.Text.Trim(), "全部");
            }
        }
    }

    public class MedicineInventoryDTO
    {
        public int MedicineInventoryId { get; set; }
        public string CardNo { get; set; }
        public string Name { get; set; }
        public string Sex { get; set; }
        public int YearsOld { get; set; }
        public string IdCard { get; set; }
        public DateTime DispenseTime { get; set; }
        public string DispenseStatus { get; set; }
        public bool IsDeleted { get; set; }
        public string DateRange { get; set; }
    }
} 